#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
const int N = 1e3 + 10;
int map[N][N];
int dis[N];
int vis[N];
int n, m;
void Dij(int x)
{
	memset(vis, 0, sizeof(vis));
	memset(dis, 0x3f, sizeof(dis));
	dis[x] = 0;
	vis[x] = 1;
	for (int i = 1; i <= n; i++) {
		if (i != x) {
			dis[i] = min(dis[i], map[x][i]+dis[x]);
		}
	}
	while (1) {
		int mi = 0x3f3f3f;
		int pos = -1;
		for (int i = 1; i <= n; i++) {
			if (!vis[i] && dis[i] < mi) {
				mi = dis[i];
				pos = i;
			}
		}
		if (pos == -1) {
			break;
		}
		for (int i = 1; i <= n; i++) {
			if (i != pos) {
				dis[i] = min(dis[i], map[pos][i]+dis[pos]);
			}
		}
		vis[pos] = 1;
	}
}
int main()
{
	memset(map, 0x3f, sizeof(map));
	scanf_s("%d%d", &m, &n);
	for (int i = 0; i < m; i++) {
		int a, b, c;
		scanf_s("%d%d%d", &a, &b, &c);
		map[a][b] = min(map[a][b], c);
		map[b][a] = map[a][b];
	}
	Dij(1);
	cout << dis[n] << endl;
	return 0;
}